package com.atguigu.exam;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author xbd
 * @create 2021-05-26 8:56
 */
public class AgeTest {
    public static void main(String[] args) {
        Age[] a = new Age[6];
        a[0] = new Age("xiaomi",25);
        a[1] = new Age("xiaomi",23);
        a[2] = new Age("xiaohua",25);
        a[3] = new Age("huju",30);
        a[4] = new Age("miaoqi",18);
        a[5] = new Age("kali",22);
        Arrays.sort(a, new Comparator<Age>() {
            @Override
            public int compare(Age o1, Age o2) {
                if(o1 instanceof Age && o2 instanceof Age){
                    Age a1 = (Age)o1;
                    Age a2 = (Age)o2;
                    if(a1.getAge() == a2.getAge()){
                        return a1.getName().compareTo(a2.getName());
                    }else{
                        return -Integer.compare(a1.getAge(),a2.getAge());
                    }
                }
                throw new RuntimeException("输入类型有误！");
            }
        });
        System.out.println(Arrays.toString(a));
    }
}

class Age{
    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Age(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Age{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}